package com.example.paper.mvp.presenter;

import android.util.Log;

import com.example.mvp.model.BaseEntity;
import com.example.mvp.presenter.BasePresenter;
import com.example.paper.brean.HttpCode;
import com.example.paper.brean.LoginBean;
import com.example.paper.http.logUtils.ToastUtils;
import com.example.paper.mvp.contract.LoginContract;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class LoginPresenter extends BasePresenter<LoginContract.LoginModel,LoginContract.LoginView>{

    public LoginPresenter(LoginContract.LoginModel mModel, LoginContract.LoginView mView) {
        super(mModel, mView);
    }

    //上传头像
    public void RequestUpload(String path){
        Map<String, Object> map = new HashMap<>();
        File file = new File(path);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("file","jhy"+System.currentTimeMillis()+".jpg",body);
        List<MultipartBody.Part> list = builder.build().parts();
        map.put("list",list);
        map.put("code", HttpCode.UPLOADIMGCODE);
        mModel.requestLogin(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }


    //修改用户信息
    public void RequestUpdate(Map<String, Object> map){
        map.put("code",HttpCode.UPDATEUSERCODE);
        mModel.requestUpload(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }


    //登陆注册
    public void RequestLogin(int phone, String pwd,int code) {
        //传入值
        Map<String, Object> map = new HashMap<>();
        map.put("code",code);
        map.put("phone", phone);
        map.put("pwd", pwd);
        mModel.requestLogin(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        //获取的数据传参
        mView.refresh(baseEntity);
    }

}
